package com.huajin.gateway.util;

import java.io.BufferedInputStream;
import java.io.BufferedReader;

import java.io.InputStreamReader;

public class SystemCommand {

    private static Runtime run = Runtime.getRuntime();

    public static String runCommand(String command) {
        StringBuffer lineBuffer = new StringBuffer();
        try {
            Process p = run.exec(command);// 启动另一个进程来执行命令
            BufferedInputStream in = new BufferedInputStream(p.getInputStream());
            BufferedReader inBr = new BufferedReader(new InputStreamReader(in));
            String lineStr;
            while ((lineStr = inBr.readLine()) != null) {//获得命令执行后在控制台的输出信息
                lineBuffer.append(lineStr);
            }
            //检查命令是否执行失败。
            if (p.waitFor() != 0) {
                if (p.exitValue() == 1)//p.exitValue()==0表示正常结束，1：非正常结束

                    System.err.println("命令" + command + "执行失败!");
            }
            System.out.println("命令" + command + "执行success!");
            inBr.close();
            in.close();
        } catch (Exception e) {
            System.out.println(e.getCause());
        }
        return lineBuffer.toString();
    }
}
